package org.tio.sitexxx.web.server.controller.gl;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.Design;
import org.tio.sitexxx.service.model.main.Question;
import org.tio.sitexxx.service.service.gl.ConsultantControllerService;
import org.tio.utils.resp.Resp;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestPath("/gl/controller/consultant")
public class ConsultantController {

    public ConsultantControllerService consultantControllerService  = ConsultantControllerService.me;

    /**
     * 未回答问题列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestPath("/question/list")
    public Resp getQuestionList(Integer pageNumber,Integer pageSize){
        if(pageNumber==null||pageSize==null)return Resp.fail();
//        Map<String ,Object>map=new HashMap<>(4);
        Page<Question> questionPage =  ConsultantControllerService.me.questionList(pageNumber,pageSize);
//        int count= ConsultantControllerService.me.count();
        if(questionPage==null)
            return Resp.fail();
//        map.put("questionPage",questionPage);
//        map.put("count",count);
        return Resp.ok(questionPage);
    }

    /**
     * 回答问题  若以前回答过则更新回答，若没有就就插入design回答记录设计为已回答
     * @param image_url  回答问题提交的图片
     * @param content    回答的内容
     * @param consultantId   顾问id
     * @param FDesignId    题目id  (历史遗留问题)
     * @return
     */
    @RequestPath("/answer/question")
    public Resp answerTheQuestion(String image_url,String content,Integer consultantId,Integer FDesignId){

        if(image_url==null||consultantId==null||content==null||FDesignId==null)return Resp.fail("参数错误");
        Design design = new Design();
        design.setContent(content);
        design.setResult(image_url);
        design.setConsultantId(consultantId);
        design.setFDesignId(FDesignId);
        Date date=new Date();
        design.setUpdatedAt(date);
        Question question=Question.dao.findById(FDesignId);
        if(question.getStatus()==0){//已回答
            Map<String,Object>temp=ConsultantControllerService.me.getDesignId(FDesignId);

            Integer id=(Integer)temp.get("id");
            System.out.println("id:"+id);
            design.setId(id);
            design.update();//根据主键更新
        }else{
            boolean result = Db.tx(()->{
                design.setCreatedAt(date);
                boolean result1 = Question.dao.findById(FDesignId).set("status",0).update();
                boolean result2 = design.save();
                if(result1&&result2)
                    return true;
                return false;
            });
            if(result)
                return Resp.ok();
            else
                return  Resp.fail();
        }
        return Resp.ok();
    }

    /**
     * 顾问已回答的问题的列表
     * @return
     */
    @RequestPath("/answered/question")
    public Resp answeredQuestionListByConsultantId(Integer consultantId,Integer pageNumber,Integer pageSize){
        if(pageNumber==null||pageSize==null||consultantId==null)return Resp.fail();
//        Map<String ,Object>map=new HashMap<>(4);
        Page<Record> questionPage = ConsultantControllerService.me.answeredQuestionListByConsultantId(consultantId,pageNumber,pageSize);
//        int count= ConsultantControllerService.me.answeredQuestionListByConsultantIdCount(consultantId);
        if(questionPage==null)
            return Resp.fail();
//        map.put("questionPage",questionPage);
//        map.put("count",count);
        return Resp.ok(questionPage);
    }
    /**
     * 顾问所有的顾问咨询订单
     * @param consultantId
     * @return
     */
    @RequestPath("/all/order")
    public Resp allOrderByConsultantId(Integer pageNumber,Integer pageSize ,Integer consultantId){
        if(consultantId ==null)
            return Resp.fail("consultantId is null");
        Page<Record> payments = new Page<>();
        payments = ConsultantControllerService.me.findPaymentsByConsultantId( pageNumber, pageSize ,consultantId);
        if(payments ==null||payments.getList().size()==0)
            return Resp.fail("无订单");
        List<Record> list = payments.getList();
        for(int i = 0 ; i<list.size(); i++){
            Record o = list.get(i);
            Integer mount = list.get(i).get("amount");
            Integer pay_amount = list.get(i).get("pay_amount");
            double amount = mount/100;
            double payAmount = pay_amount/100;
            list.get(i).set("pay_amount",payAmount);
            list.get(i).set("amount",amount);
        }
        return Resp.ok(payments);
    }

}
